/*!
 * http request
 * Copyright(c) purenode
 * @author yangjian102621@gmail.com
 * MIT Licensed
 */

"use strict"

/**
 * 初始化模块依赖
 */
var url = require("url");
var queryString = require("querystring");

function Request(req, app) {

	this.req = req;
	this.app = app;
}

//获取app对象
Request.prototype.getApp = function() {

	return this.app;
}

//获取原生的http request对象
Request.prototype.getOriReq = function() {
	return this.req;
}

//获取指定参数
Request.prototype.getParameter = function(name) {

	var params = url.parse(this.req.url, true).query;
	return params[name];
}

//获取所有参数
Request.prototype.getParameters = function() {

	return url.parse(this.req.url, true).query;
}

//获取post数据
Request.prototype.getPostData = function(name) {
	var data = queryString.parse(this.app.getConfig("POST_DATA"));
	return data[name];
}

//获取post参数数组
Request.prototype.getPostDataArray = function(name) {
	var data = queryString.parse(this.app.getConfig("POST_DATA"));
	var result = [];
	for ( var key in data ) {
		if ( key.indexOf(name+"[") != -1 ) {
			var __key = key.replace(name+"[", "");
			__key = __key.replace("]", "");
			result[__key] = data[key];
		}
	}
	return result;
}

module.exports = Request;
